<!-- <button id="google_connect" style="">Login with your google account</button> -->
<li style="float:right">
	  
<img id="google_connect" src='images/google_signin.png' />


<script type="text/javascript">
//////////////////////////////////
// AUTHENTICATING WITH GOOGLE ////
//////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// AUTHENTICATING WITH GOOGLE //////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
(function() {
var GOOGLE_AUTH_URL = "https://accounts.google.com/o/oauth2/auth";
var GOOGLE_CLIENT_ID = "1059780877991-60pnuohker160c9eff0sob0r1ob0nvd3.apps.googleusercontent.com";
var PLUS_ME_SCOPE = "https://www.googleapis.com/auth/plus.me";

connectGoogle = function() { 

	var req = {
	 "authUrl" : GOOGLE_AUTH_URL,
	 "clientId" : GOOGLE_CLIENT_ID,
	 "scopes" : [ PLUS_ME_SCOPE ], //"scopes" : [ PLUS_ME_SCOPE, PLUS_ANOTHER_SCOPE ]
	 "redirectURI" : "<?PHP bloginfo("template_url");?>/lib/oauth-site/" //If necessary this url must end with '/'
	};
	
	initiateOAuthLogin(req);
};

$("#google_connect").click(connectGoogle);
 
})();

 
/* used codes from https://developers.google.com/accounts/docs/OAuth2Login */

function initiateOAuthLogin(req)
{
	var url= req.authUrl+'?response_type=token&client_id='+req.clientId;
	url += '&scope='+req.scopes.join(' ');
	url += '&redirect_uri='+encodeURIComponent(req.redirectURI);
	
	//console.log(url);
	oauth2= window.open(url,'popupWindow','width=800,height=600');
}

function doOAuthLogin(params)
{
	if( params.access_token)
	{	
		// As I have no way to know if the user is logged into google or not so time-till-valid param is ignored
		
		//alert( params['access_token']);
		jQuery.ajax({
			url: "<?PHP bloginfo("template_url");?>/lib/oauth-site/user_login.php?id=2013&access_token="+params['access_token'],
			dataType: 'json',
			success: function(data) { 
			//console.log(data); alert('done');
				var r=confirm("You have successfully logged in.\n\nPlease note that, you are also logged into your Gmail account. Do you want to logout from Gmail now?\n\nOK= Logout from Gmail. \nCancel= Continue with Gmail");
				if (r==true)
				{
					window.location = "https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue="+window.location.href;
				}
				//else if(!!data.nextDestination)
				//	window.location= data.nextDestination;					
				else
					document.location.reload(true);
			
			},
			error: function (data, textStatus, jqXHR) { alert('Error happened: '+textStatus); document.location.reload(true); }
		});
	}	
	else if( params.error)
		alert( params['error']);
	else
		alert( 'Some error is happening. Please try again or inform site administrator.');
}
</script>
</li>